<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="100px">
      <el-form-item label="版本号" prop="version">
        <el-input v-model="dataForm.version" placeholder="请输入版本号，如：1.0.0"></el-input>
      </el-form-item>
      <el-form-item label="更新内容" prop="updateContent">
        <el-input type="textarea" v-model="dataForm.updateContent" :rows="4" placeholder="请输入更新内容"></el-input>
      </el-form-item>
      <el-form-item label="更新类型" prop="updateType">
        <el-select v-model="dataForm.updateType" placeholder="请选择更新类型">
          <el-option label="新功能" value="feature"></el-option>
          <el-option label="修复" value="bugfix"></el-option>
          <el-option label="优化" value="optimize"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="发布日期" prop="releaseDate">
        <el-date-picker
          v-model="dataForm.releaseDate"
          type="datetime"
          placeholder="选择发布日期"
          value-format="yyyy-MM-dd HH:mm:ss">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="1">已发布</el-radio>
          <el-radio :label="0">未发布</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="创建人" prop="creator">
        <el-input v-model="dataForm.creator" placeholder="请输入创建人"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: 0,
        version: '',
        updateContent: '',
        updateType: '',
        releaseDate: '',
        status: 0,
        creator: ''
      },
      dataRule: {
        version: [
          { required: true, message: '版本号不能为空', trigger: 'blur' }
        ],
        updateContent: [
          { required: true, message: '更新内容不能为空', trigger: 'blur' }
        ],
        updateType: [
          { required: true, message: '更新类型不能为空', trigger: 'change' }
        ],
        releaseDate: [
          { required: true, message: '发布日期不能为空', trigger: 'change' }
        ],
        status: [
          { required: true, message: '状态不能为空', trigger: 'change' }
        ],
        creator: [
          { required: true, message: '创建人不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`algorithmUpdateLog/algorithmUpdateLog/queryWormholeGalaxyAlgorithmUpdateLog`),
            method: 'post',
            data: this.$http.adornData({
              "id":id
            })
          }).then(({data}) => {
            if (data && data.success) {
              this.dataForm.version = data.data.version
              this.dataForm.updateContent = data.data.updateContent
              this.dataForm.updateType = data.data.updateType
              this.dataForm.releaseDate = data.data.releaseDate
              this.dataForm.status = data.data.status
              this.dataForm.creator = data.data.creator
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/algorithmUpdateLog/algorithmUpdateLog/${!this.dataForm.id ? 'createWormholeGalaxyAlgorithmUpdateLog' : 'createWormholeGalaxyAlgorithmUpdateLog'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'version': this.dataForm.version,
              'updateContent': this.dataForm.updateContent,
              'updateType': this.dataForm.updateType,
              'releaseDate': this.dataForm.releaseDate,
              'status': this.dataForm.status,
              'creator': this.dataForm.creator
            })
          }).then(({data}) => {
            if (data && data.success) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-select {
  width: 100%;
}
</style>
